package jzqb.controller;

import haha.life.common.CutPageBean;
import haha.life.normal.TokenUtils;
import jzqb.bo.CollectDataBo;
import jzqb.common.Result;
import jzqb.service.CollectDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/CollectData")
public class CollectDataController {


    @Autowired
    CollectDataService collectDataService;

    //新建收藏分类数据
    @PostMapping(value = "/insertCollectData")
    public Result insertCollectClassify(@RequestBody CollectDataBo collectDataBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectDataBo.setUserID(userId);
        if (collectDataBo.getCollectClassifyID()=="" || collectDataBo.getCollectClassifyID()==null){
            return Result.fail("请选择收藏分类");
        }
        Result res = collectDataService.insertCollectData(collectDataBo);
        return res;
    }

    //删除收藏分类数据
    @PostMapping(value = "/deleteCollectData")
    public Result deleteCollectData(@RequestBody CollectDataBo collectDataBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectDataBo.setUserID(userId);
        Boolean res = collectDataService.deleteCollectData(collectDataBo);
        if (res) {
            return Result.succ("删除收藏数据成功");
        }
        return Result.fail("删除收藏数据失败");
    }

    //根据收藏分类的ID 获取收藏分类的数据
    @PostMapping(value = "/getCollectDataByClassyId")
    public Result getCollectDataByClassyId(@RequestBody CollectDataBo collectDataBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectDataBo.setUserID(userId);
        CutPageBean<CollectDataBo> res = collectDataService.getCollectDataByClassyId(collectDataBo);
        return Result.succ(res);
    }

    //查询出所有的收藏数据并且带有所有的list
    @PostMapping(value = "/getAllCollectionData")
    public Result getAllCollectionData(@RequestBody CollectDataBo collectDataBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectDataBo.setUserID(userId);
        CutPageBean<CollectDataBo> res = null;
        if (collectDataBo.getCollectClassifyID() != null && collectDataBo.getCollectClassifyID()!="") {
            res = collectDataService.getCollectDataByClassyId(collectDataBo);
        } else {
            res = collectDataService.getAllCollectionData(collectDataBo);
        }
        return Result.succ(res);
    }
}
